﻿using Beryl.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls.WebParts;

namespace Beryl.Model
{
    //裁切报工

    public class CuttingRecord
    {
        private static CuttingRecord instance = null;
        private static readonly object uniqueLock = new object();

        public static CuttingRecord Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new CuttingRecord();
                    }
                }
                return instance;
            }
        }

        public CuttingRecord() { }


        //裁切报工查询
        public List<string> getCaiQie(string data,string Data)
        {
            List<string> list = new List<string>();
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string strPicYZ1 = "";
            try
            {
                string sql = @"select a.cMOLotNo,a.cBatchNumber,a.dEffWidth,a.dLotQty,b.cSpecifucation,c.dEffWidth1,c.dLength1,c.dReqQty,a.cMatNo,a.cProductDepNo from APS_ProLotOrder a left join APS_MatData_V b on a.cMatNo = b.cMatNo left join (select cOrderNo,SUM(dReqQty) as dReqQty,SUM(dPlanQty * dWidth) as dEffWidth1,MAX(dLength) as dLength1 from APS_ValidBOM where cOrderType = 3 group by cOrderNo) c on c.cOrderNo = a.cMOLotNo where a.cBarCode = '{0}'";

                string sql1 = string.Format(sql, data);//未切料信息
                string strPicYZ = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql1));

                string sql2 = @"SELECT cSpecifution, dWidth, dPlanQty as dPiece, dLength, cOrderNo,dReqQty,dWidth * dPlanQty as dEffWidth FROM APS_ValidBOM where cOrderNo = '{0}' order by dWidth";

                Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(strPicYZ);//解析为数组
                if(jsonArr.Count > 0)
                {
                    var k = jsonArr[0];
                    string sql3 = string.Format(sql2, k["cMOLotNo"].ToString());//查询裁切已切料信息
                    strPicYZ1 = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql3));//转为json 
                    if (strPicYZ1.Equals("[]"))
                    {
                        list.Add("此流传单号错误");
                        //CommonMethod.Instance.InsertLog(name, list[0], CommonMethod.LogType.Info, Data, "裁切报工查询流传单错误");
                        CommonMethod.Instance.InsertLog1(data, list[0], Data, CommonMethod.LogType.Error, name, "", true);
                        return list;
                    }
                }else
                {
                    list.Add("此流传单号错误");
                    //CommonMethod.Instance.InsertLog(name, list[0], CommonMethod.LogType.Info, Data, "裁切报工查询流传单错误");
                    CommonMethod.Instance.InsertLog1(data, list[0], Data, CommonMethod.LogType.Error, name, "", true);
                    return list;
                }
                
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("查询裁切报工", strPicYZ);
                dic.Add("查询裁切已切料信息", strPicYZ1);
                string json = JsonConvert.SerializeObject(dic);
                json = CommonMethod.Instance.JosnChange(json);
                list.Add(json);
            }
            catch (Exception e)
            {
                list.Add(e.Message);
                //CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Error, Data, "裁切报工查询流传单信息错误");
                CommonMethod.Instance.InsertLog1(data, list[0], Data, CommonMethod.LogType.Error, name, "", true);
                return list;
            }
            //CommonMethod.Instance.InsertLog(name, list[0], CommonMethod.LogType.Info, Data, "裁切报工查询流传单信息");
            CommonMethod.Instance.InsertLog1(data, "裁切报工查询流传单信息" + list[0], Data, CommonMethod.LogType.Info, name, "", true);
            return list;
        }

        //计算剩余量
        public List<string> sunFrom(string para, string Data)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            List<string> list = new List<string>();
            try
            {
                Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);//解析为数组
                var k = jsonArr[0];
                float dLotQty = float.Parse(k["dLotQty"].ToString());
                float dEffWidth = float.Parse(k["dEffWidth"].ToString());
                float dEffWidth1 = float.Parse(k["dEffWidth1"].ToString());
                float dLength1 = float.Parse(k["dLength1"].ToString());
                var dLength = Math.Round((dLotQty / dEffWidth * 1000),3);
                var dW = Math.Round((dEffWidth - dEffWidth1),3); //未切料剩余有效宽
                var dL = Math.Round((dLength - dLength1),3); //未切料剩余长度
                if (dL <= 0 && dW <= 0)
                {
                    var mJ = Math.Round((dW * dL * 0.001), 6);
                    dic.Add("剩余长度", dL.ToString("N3"));
                    dic.Add("剩余宽度", dW.ToString("N3"));
                    dic.Add("剩余面积", mJ.ToString("N6"));
                }
                if (dL <= 0 && dW > 0)
                {
                    var mJ = Math.Round((dW * dLength * 0.001), 6);
                    dic.Add("剩余长度", dLength.ToString("N3"));
                    dic.Add("剩余宽度", dW.ToString("N3"));
                    dic.Add("剩余面积", mJ.ToString("N6"));//未切料剩余面积
                }
                if (dL > 0)
                {
                    var mJ = dEffWidth * dL * 0.001;
                    dic.Add("剩余长度", dL.ToString("N3"));
                    dic.Add("剩余宽度", dEffWidth.ToString("N3"));
                    dic.Add("剩余面积", mJ.ToString("N6"));//未切料剩余面积
                }
                string json = JsonConvert.SerializeObject(dic);
                json = CommonMethod.Instance.JosnChange(json);
                list.Add(json);
            }
            catch(Exception e)
            {
                list.Add(e.Message);
                //CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Error, Data, "裁切报工计算余量错误");
                CommonMethod.Instance.InsertLog1("",list[0], Data, CommonMethod.LogType.Error, name, "", true);
                return list;
            }
            //CommonMethod.Instance.InsertLog(name, list[0], CommonMethod.LogType.Info, Data, "裁切报工计算余量信息");
            CommonMethod.Instance.InsertLog1("", "裁切报工计算余量信息" +list[0], Data, CommonMethod.LogType.Error, name, "", true);
            return list;
        }



        //裁切报工提交逻辑
        public string summitCQ(string data, string Data)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(data);//解析为数组
            var k = jsonArr[0];
            try
            {
                //验证提交的长度宽度面积是否大于0
                float dWidth = float.Parse(k["dWidth"].ToString());
                float dLength = float.Parse(k["dLength"].ToString());
                float dArea = float.Parse(k["dArea"].ToString());
                //if(dWidth <= 0 || dLength <= 0 || dArea <= 0)
                //{
                //    return "未切物料存在为0值";
                //}


                //插入数据到未切料库存表PMS_UnCutStock
                string sql = "insert into PMS_UnCutStock(cMOLotNo, cBatchNumber, cMatNo, cProductDepNo, cSpecifucation, dArea, dLength, dWidth, isSync, createTime) " +
                    "values('" + k["cMOLotNo"].ToString() + "','" + k["cBatchNumber"].ToString() + "','" + k["cMatNo"].ToString() + "','" + k["cProductDepNo"].ToString() + "','" + k["cSpecifucation"].ToString() + "'," + Double.Parse(k["dArea"].ToString()) + "," + Double.Parse(k["dLength"].ToString()) + "," + Double.Parse(k["dWidth"].ToString()) + ",'" + k["isSync"].ToString() + "','" + k["ReportTime"].ToString() + "')";
                //插入数据到生产报工表APS_PhistoryTable
                string sql1 = string.Format(@"insert into APS_PhistoryTable (cMOLotNo,Operator,GNumber,ReportTime,NYR,isSyncReport) values ('{0}','{1}','{2}','{3}','{4}','0')", k["cMOLotNo"].ToString(), k["Operator"].ToString(), k["GNumber"].ToString(), k["ReportTime"].ToString(), k["NYR"].ToString());
                //将裁切单批次当前状态，设置为已完成生产cStatusNo = 0090
                string sql2 = string.Format(@"update APS_ProLotOrder set cStatusNo = '0090' where cMOLotNo = '{0}'", k["cMOLotNo"].ToString());
                //将裁切批次表报工状态更新为已完成
                string sql3 = string.Format(@"update APS_CutProLotOrder set isReport = '2' where cOrderNo = '{0}'", k["cMOLotNo"].ToString());

                string sql4 = sql + ";" + sql1 + ";" + sql2 + ";" + sql3;

                SqlHelper.IniConnString();
                var connstr = SqlHelper.GetConnSting();
                SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, sql4);
                LogUtil.Log(name + sql4);//将方法名写进
            }
            catch (Exception e)
            {
                //CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Error, Data, "裁切报工提交失败");
                CommonMethod.Instance.InsertLog1(k["cMOLotNo"].ToString(), e.Message, Data, CommonMethod.LogType.Error, name, "", true);
                return e.Message;
            }
            //CommonMethod.Instance.InsertLog(name, "[1]", CommonMethod.LogType.Info, Data, "裁切报工提交成功");
            CommonMethod.Instance.InsertLog1(k["cMOLotNo"].ToString(), "裁切报工提交成功", Data, CommonMethod.LogType.Error, name, "", true);
            return "[1]";
        }

    }
}